# from django.contrib.postgres import serializers
from django.core import serializers
from django.http import JsonResponse, HttpResponse
from django.shortcuts import render
from django.views import View

from apps.goods.models import GoodsCategory, SKU,SKUImage

# from utils.goods import get_breadcrumb
import json

class ListView(View):

    def get(self,request,category_id):
        # 1.接收参数
        # 排序字段
        ordering=request.GET.get('ordering')
        # 每页多少条数据
        page_size=request.GET.get('page_size')
        # 要第几页数据
        page=request.GET.get('page')

        # 2.获取分类id
        # 3.根据分类id进行分类数据的查询验证
        try:
            category=GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExist:
            return JsonResponse({'code':400,'errmsg':'参数缺失'})
        # 4.获取面包屑数据


        # 5.查询分类对应的sku数据，然后排序，然后分页
        skus=SKU.objects.filter(category=category,is_launched=True).order_by(ordering)
        # 分页
        from django.core.paginator import Paginator
        # object_list, per_page
        # object_list   列表数据
        # per_page      每页多少条数据
        paginator=Paginator(skus,per_page=page_size)

        #获取指定页码的数据
        page_skus=paginator.page(page)

        sku_list=[]
        #将对象转换为字典数据
        for sku in page_skus.object_list:
            sku_list.append({
                'id':sku.id,
                'name':sku.name,
                'price':sku.price,
                'default_image_url':sku.default_image.url,
                'comment_count':sku.comments,
                'sales':sku.sales,
                'create_time':sku.create_time
            })

        # 获取总页码
        total_num = paginator.num_pages

        # 6.返回响应
        return JsonResponse({'code':0,'errmsg':'ok','list':sku_list,'count':total_num})
class search_Goods(View):
        def get(self, request,category_id):
            # 1.接收参数
            # 排序字段
            ordering = request.GET.get('ordering')
            # 每页多少条数据
            page_size = request.GET.get('page_size')
            # 要第几页数据
            page = request.GET.get('page')
            key_words=request.GET.get('word')
            # 2.获取分类id
            # 3.根据分类id进行分类数据的查询验证
            try:
                category = GoodsCategory.objects.get(id=category_id)
            except GoodsCategory.DoesNotExist:
                return JsonResponse({'code': 400, 'errmsg': '参数缺失'})
            # 4.获取面包屑数据

            # 5.查询分类对应的sku数据，然后排序，然后分页
            skus = SKU.objects.filter(category=category, is_launched=True,name__icontains=key_words).order_by(ordering)
            # 分页
            from django.core.paginator import Paginator
            # object_list, per_page
            # object_list   列表数据
            # per_page      每页多少条数据
            paginator = Paginator(skus, per_page=page_size)

            # 获取指定页码的数据
            page_skus = paginator.page(page)

            sku_list = []
            # 将对象转换为字典数据
            for sku in page_skus.object_list:
                sku_list.append({
                    'id': sku.id,
                    'name': sku.name,
                    'price': sku.price,
                    'default_image_url': sku.default_image.url,
                    'comment_count': sku.comments,
                    'sales': sku.sales
                })

            # 获取总页码
            total_num = paginator.num_pages

            # 6.返回响应
            return JsonResponse({'code': 0, 'errmsg': 'ok', 'list': sku_list, 'count': total_num})

from utils.goods import get_breadcrumb
from utils.goods import get_goods_specs

class DetailView(View):
    def get(self,request,sku_id):
        sku=SKU.objects.get(id=sku_id)
        skuid=sku.id
        price=sku.price
        pics=[]
        image1=list(SKUImage.objects.filter(sku_id=sku_id))
        name=sku.caption
        for image in image1:
            image_item=str(image.image)

            pics.append(image_item)
        sku_list={
            'pics': pics,
                'sku':skuid,
            'price':price,
            'name':name
            # 'name',name
        }
        # pics=list(SKU.objects.filter(skuimage=1))
        return JsonResponse({'code': 0, 'errmsg': 'ok'
                                , 'spu_info': sku_list
                             },safe=False)


